package com.huantai.common.exception;

import java.util.ArrayList;
import java.util.List;

import com.huantai.common.message.BusinessMessage;

/**
 * 自定义业务异常类
 * 
 * @author Ma Chao
 * @version 2018年4月17日 下午5:27:19
 */
public class BusinessException extends SystemException {
    private static final long serialVersionUID = 1L;
    /** 业务异常信息列表 */
    private List<BusinessMessage> businessMessages; // NOSONAR

    /**
     * 构造函数
     * 
     * @param message 异常消息
     */
    public BusinessException(String message) {
        super(message);
        this.addBusinessMessage(new BusinessMessage(message));
    }

    /**
     * 构造函数
     * 
     * @param message 异常消息
     * @param cause 异常原因
     */
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        if (cause != null) {
            this.addBusinessMessage(new BusinessMessage(message));
        }
    }

    /**
     * 构造函数
     * 
     * @param businessMessages 业务异常信息列表
     */
    public BusinessException(List<BusinessMessage> businessMessages) {
        super(fetchMessage("业务校验失败", businessMessages));
        this.businessMessages = businessMessages;
    }

    /**
     * 获取：业务异常信息列表
     */
    public List<BusinessMessage> getBusinessMessages() {
        return this.businessMessages;
    }

    /**
     * 添加业务异常信息
     * 
     * @Title: addBusinessMessage
     * @param message
     */
    public void addBusinessMessage(String message) {
        this.addBusinessMessage(new BusinessMessage(message));
    }

    /**
     * 添加业务异常信息
     * 
     * @Title: addBusinessMessage
     * @param message
     */
    public void addBusinessMessage(BusinessMessage message) {
        if (null == this.businessMessages) {
            this.businessMessages = new ArrayList<>();
        }
        this.businessMessages.add(message);
    }

    /**
     * 添加业务异常信息
     * 
     * @Title: addBusinessMessage
     * @param messages
     */
    public void addBusinessMessage(List<BusinessMessage> messages) {
        if (null == this.businessMessages) {
            this.businessMessages = new ArrayList<>();
        }
        this.businessMessages.addAll(messages);
    }
}
